/**
 *
 * 横向导航栏
 */


'use strict';
var React = require('react');
class HkfsHomeBar extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            active: 0,
            login:false
        };
        this.getAccountClass=this.getAccountClass.bind(this);
        this.getBarItemClass=this.getBarItemClass.bind(this);
        this.getBarItemActiveCount=this.getBarItemActiveCount.bind(this);
    }

    //获取当前菜单栏a标签样式
    getBarItemClass(key){
        if(key==this.state.active){
            return 'active';
        }
        else{
            return 'normal';
        }
    }

    //设置当前active页
    getBarItemActiveCount(){
        if(window){
            var UrlInformation=require('../../common/CommonUrlOperation.js');
            var activeCount=0;
            switch (UrlInformation.pathname){
                case "/":
                    activeCount=0;
                    break;
                case "/about":
                    activeCount=1;
                    break;
                case "/viewPoint":
                    activeCount=2;
                    break;
                case "/products":
                    activeCount=3;
                    break;
                case "/productDetail":
                    activeCount=3;
                    break;
                case "/myAccount":
                    activeCount=4;
                    break;
                default :
                    activeCount=-1;
                    break;

            }
            this.setState({
                active: activeCount
            });
        }
    }

    //判断用户登录状态
    componentDidMount() {
        var self=this;
        var random= Math.random();
        //渲染完毕后才执行
        this.getBarItemActiveCount();
        reqwest({
            url: `/api/checkLogin?version=${random}`,
            method: 'get',
            contentType: 'application/json'
        })
            .then(function (data) {
                if(data.userId) {
                    self.setState({
                        login:true
                    });
                }
            })
            .fail(function (err, msg) {
                alert('服务器出错');
            });
    }

    getAccountClass(key){
        if(this.state.login){
            return this.getBarItemClass(key);
        }
        else{
            return 'hide'
        }
    }


    render() {
      return (
          <div className="HkfsHomeBar_Container">
            <div>
                <img src="/images/pc/header_logo.png"/>
            </div>
            <div className="HkfshomeBar_Menu">
                <a className={this.getBarItemClass(0)} href="/">首页</a>
                <a className={this.getBarItemClass(1)} href="/about">关于我们</a>
                <a className={this.getBarItemClass(2)} href="/viewPoint">华康视点</a>
                <a className={this.getBarItemClass(3)} href="/products">选择产品</a>
                <a className={this.getAccountClass(4)} href="/myAccount">我的账户</a>
            </div>
        </div>
      );
    }
}

module.exports = HkfsHomeBar;